package cn.edu.scau.cmi.yangzemin.comprehension.client;

import java.util.List;


import org.springframework.context.ApplicationContext;

import cn.edu.scau.cmi.yangzemin.comprehension.dao.StudentDAO;
import cn.edu.scau.cmi.yangzemin.comprehension.dao.TeacherDAO;
import cn.edu.scau.cmi.yangzemin.comprehension.domain.Student;
import cn.edu.scau.cmi.yangzemin.comprehension.domain.Teacher;
import cn.edu.scau.cmi.yangzemin.comprehension.util.ApplicationContextUtil;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
public class ComprehensionHibernateUIController {
	
	private static final StudentDAO studentDAO = (StudentDAO)getApplicationContext().getBean("studentDAO");
	private static final TeacherDAO teacherDAO = (TeacherDAO)getApplicationContext().getBean("teacherDAO");

    @FXML	private Button addStudent;
    @FXML	private Button stuList;
    @FXML	private Button modStudent;
    @FXML	private TextField stuIdOfTeacher;
    @FXML	private Button teaModStu;
    @FXML	private TextField teaIdOfTeacher;
    @FXML	private Button teaList;
    @FXML	private Button stuModTea;
    @FXML	private TextField teaIdOfStudent;
    @FXML	private Button addTeacher;
    @FXML	private Button delTeacher;
    @FXML	private TextField stuIdOfStudent;
    @FXML	private Button teaChoiceStu;
    @FXML	private Button modTeacher;
    @FXML	private Button delStudent;
    @FXML	private TextArea stuText;
    @FXML	private TextField stuNameOfStudent;
    @FXML	private TextArea teaText;
    @FXML	private TextField teaNameOfTeacher;
    @FXML	private Button stuChoiceTea;

	private static ApplicationContext getApplicationContext()
	{
		return ApplicationContextUtil.getInstance();
	}
    public void init() {     
     addStudent.setOnAction(this::AddStudent);   
     delStudent.setOnAction(this::DelStudent);
     modStudent.setOnAction(this::ModStudent);
     stuChoiceTea.setOnAction(this::StuChoiceTea);
     stuModTea.setOnAction(this::StuModTea);
     stuList.setOnAction(this::StuList); 
     
     addTeacher.setOnAction(this::AddTeacher);   
     delTeacher.setOnAction(this::DelTeacher);
     modTeacher.setOnAction(this::ModTeacher);
     teaChoiceStu.setOnAction(this::TeaChoiceStu);
     teaModStu.setOnAction(this::TeaModStu);
     teaList.setOnAction(this::TeaList);
    }
              
    public void	AddStudent(ActionEvent event) {
    	Student student = new Student();
    	student.setName(stuNameOfStudent.getText());
    	student.setNumber((long)Long.parseLong(stuIdOfStudent.getText()));
    	studentDAO.save(student);
    	stuText.setText("新添加学生: "+"\n");
    	stuText.setText(stuText.getText()+student.toString()+"\n");
    }    
    
    public void	DelStudent(ActionEvent event) {
    	String name = stuNameOfStudent.getText();
    	Student student = (Student) studentDAO.findByName(name);
    	studentDAO.delete(student);   
    	stuText.clear();
    	stuText.setText("删除的学生信息:"+"\n");
    	stuText.setText(stuText.getText()+student.toString()+"\n");
    }
    
    public void ModStudent(ActionEvent event) {
    	Student student = (Student) studentDAO.findById(Long.parseLong(stuIdOfStudent.getText()));
    	stuText.clear();
        stuText.setText("修改前的原信息:"+"\n");
        stuText.setText(stuText.getText()+student.toString()+"\n");
    	student.setName(stuNameOfStudent.getText());
        studentDAO.update(student);    
        stuText.setText(stuText.getText()+"修改的新信息:"+"\n");
        stuText.setText(stuText.getText()+student.toString());
    } 
    
    public void StuChoiceTea(ActionEvent event) {
    	Teacher teacher = (Teacher) teacherDAO.findById(Long.parseLong(teaIdOfStudent.getText()));
    	String name = stuNameOfStudent.getText();
		Student student = studentDAO.findByName(name);
		student.setTeacher(teacher);
		studentDAO.update(student);
		stuText.setText("学生选择导师: "+"\n");
	    stuText.setText(stuText.getText()+student.toString()+" 导师名称 : "+teacher.getName());
    }
    
    public void StuModTea(ActionEvent event) {
    	String name = stuNameOfStudent.getText();
		Student student = studentDAO.findByName(name);
		stuText.clear();
		stuText.setText("学生的原来导师: "+"\n");
		stuText.setText(stuText.getText()+student.toString()+" 导师名称 : "+student.getTeacher().getName()+"\n");
		Teacher teacher = (Teacher) teacherDAO.findById(Long.parseLong(teaIdOfStudent.getText()));
		student.setTeacher(teacher);
		studentDAO.update(student);
	    stuText.setText(stuText.getText()+ "学生选择新的导师: "+"\n"+student.toString()+" 导师名称 : "+teacher.getName());
    }
    public void StuList(ActionEvent event) {
		List students = studentDAO.findAll();
		if (students.size() == 0)
		{
			stuText.setText("无学生信息");
		}
		else
		{
			stuText.setText("所有学生的信息如下:"+"\n");
			for (Object student : students)
			{
				stuText.setText(stuText.getText()+student.toString()+"\n");
			}
		}
    }
    
    
    public void	AddTeacher(ActionEvent event) {
    	Teacher teacher = new Teacher();
    	teacher.setName(teaNameOfTeacher.getText());
    	teacherDAO.save(teacher);
    	teaText.clear();
    	teaText.setText(teacher.toString());
    }      
    
    public void	DelTeacher(ActionEvent event) {
       	String name = teaNameOfTeacher.getText();    	
    	Teacher teacher = (Teacher) teacherDAO.findByName(name).get(0);
		teacherDAO.delete(teacher);
    	teaText.clear();
    	teaText.setText("删除教师的信息:"+"\n");
    	teaText.setText(teaText.getText()+teacher.toString());
    }          
    
    public void ModTeacher(ActionEvent event) {
    	Teacher teacher = (Teacher) teacherDAO.findById(Long.parseLong(teaIdOfTeacher.getText()));
    	teaText.clear();
    	teaText.setText("修改前的原信息:"+"\n");
    	teaText.setText(teaText.getText()+teacher.toString()+"\n");
    	teacher.setName(teaNameOfTeacher.getText());
		teacherDAO.update(teacher);
		teaText.setText(teaText.getText()+"修改的新信息:"+"\n");
		teaText.setText(teaText.getText()+teacher.toString());
    }
    
    public void TeaChoiceStu(ActionEvent event){
    	String name = teaNameOfTeacher.getText();
		Teacher teacher  = (Teacher)teacherDAO.findByName(name).get(0);
       	Student student = (Student) studentDAO.findById(Long.parseLong(stuIdOfTeacher.getText()));
       	student.setTeacher(teacher);
		studentDAO.update(student);
		teaText.setText("导师选择学生: "+"\n");
	    teaText.setText(teaText.getText()+teacher.toString()+"选择学生 : "+student.getName());

    }
    
    public void TeaModStu(ActionEvent event) {
    	String name = teaNameOfTeacher.getText();
		Teacher teacher  = (Teacher)teacherDAO.findByName(name).get(0);		
       	Student student = (Student) studentDAO.findById(Long.parseLong(stuIdOfTeacher.getText()));
       	student.setTeacher(teacher);
		studentDAO.update(student);
		teaText.setText("导师选择的学生: "+"\n");
	    teaText.setText(teaText.getText()+teacher.toString()+"选择学生 : "+student.getName());
    }    
    public void TeaList(ActionEvent event) {
    	List teachers = teacherDAO.findAll();
		if (teachers.size() == 0)
		{
			teaText.setText("无教师信息");
		}
		else
		{
			teaText.setText("所有教师的信息如下:"+"\n");
			for (Object teacher : teachers)
			{
				teaText.setText(teaText.getText()+teacher.toString()+"\n");
			}
		}
    }
    
}
